<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
    <title>PHP Class Structure Documentation</title>
    <link href="css/reset.css" media="" rel="stylesheet" type="text/css" />
    <link href="css/styles.css" media="screen" rel="stylesheet" type="text/css" />
    <link href="css/datatable.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<body style="padding: 1em 2em;">
    <?php
    ini_set('display_errors', true);
    error_reporting(E_ALL);
    
    require_once dirname(__FILE__) . '/../examples/_include.php';
    
    //==========================================================================
    
    $str_table_source = 'test';
    
    $obj_datagateway = new Common_DataGateway_Database();
    $obj_datagateway->prepare_resource('sample', array(
        'adapter' => 'pdo_mysql',
        'host' => 'localhost',
        'username' => 'root',
        'password' => 'password',
        'dbname' => 'sample'
    ));
    $obj_datagateway->set_default_resource('sample');
    
    $obj_dataobject = new Common_DataGateway_Database_Object($obj_datagateway);

   
    
    $aa = array('A', 'B', 'C');

    $bb = array('Z', 'Y', 'X');
    
    for($i = 1; $i <= 20; $i++) {
       
        $name = strtoupper(Utility_StringFunctions::random(8));
        $group_id = rand(0,2);
        $group = $aa[$group_id];
        $cat_id = rand(0,2);
        $cat = $bb[$cat_id];
        
        $v = rand(1,10000);
        if(rand(1,5) == 3) {
            $v = $v * -1;
        }
        $p = rand(1,99) / 100;
        $c = rand(10,200) . '.' . (rand(10,99)/100);
        $b = rand(0,1);
        
        $d = rand(1980,2010).'-'.
            str_pad(rand(1,12), 2, '0', STR_PAD_LEFT).'-'.
            str_pad(rand(1,28), 2, '0', STR_PAD_LEFT).' '.
            str_pad(rand(0,23), 2, '0', STR_PAD_LEFT).':'.
            str_pad(rand(0,59), 2, '0', STR_PAD_LEFT).':'.
            str_pad(rand(0,59), 2, '0', STR_PAD_LEFT);
        $s = rand(0,60000);
        $e = rand(0, 5);
        
        $l = array();
        $r = rand(3,6);
        for($x = 0; $x <= $r; $x++) {
            $l[] = rand(30,90);
        }
        $l = implode('|', $l);
        
        $obj_datagateway->insert('test', array(
            'id' => $i,
            'name' => $name,
            'group_id' => $group_id,
            'group_name' => $group,
            'category_id' => $cat_id,
            'category_name' => $cat,
            'value' => $v,
            'percentage' => $p,
            'currency' => $c,
            'enumeration' => $e,
            'boolean' => $b,
            'date' => $d,
            'seconds' => $s,
            'list' => $l
        ));
    }
    die;

    ini_set('memory_limit', '1028M');
    ini_set('max_execution_time', 0);
    
    $arr_data = array();
    $obj_model = new Common_DataTable_Model_DatabaseTable($obj_datagateway, $str_table_source);
    $obj_results = $obj_model->getData();
    
    foreach($obj_results as $arr) {
        $arr_data[] = $arr;
    }
    
    $arr_columns = array(
        'id' => array(
            'label' => 'ID',
            'type' => 'identifier',
            'class' => 'i',
        ),
        'name' => 'Name',
        'group_name' => 'Group',
        'category' => array(
            'label' => 'Category',
            'source' => 'category_name'
        ),
        'value' => array(
            'label' => 'Value',
            'type' => 'number',
            'class' => 'number',
            'format' => 'number'
        ),
        'percentage' => array(
            'label' => 'Percentage',
            'type' => 'number',
            'class' => 'number',
            'format' => 'percentage',
            'format_options' => array(
                'scale' => 100
            )
        ),
        'currency' => array(
            'label' => 'Currency',
            'type' => 'number',
            'class' => 'number',
            'format' => 'currency',
            'format_options' => array(
                'precision' => 2
            )
        ),
        'boolean' => array(
            'label' => 'Boolean',
            'type' => 'boolean',
            'class' => 'number',
            'format' => 'boolean'
        ),
        'date' => array(
            'label' => 'Date',
            'type' => 'date',
            'class' => 'date',
            'format' => 'date', 
            'format_options' => array(
                'format' => 'd M Y, H:i:s'
            )
        ),
        'seconds' => array(
            'label' => 'Seconds',
            'type' => 'number',
            'class' => 'number',
            'format' => 'seconds'
        )
    );
    
    $arr_headers = array($arr_columns);
    
    $arr_rendered = array();
    
    //--------------------------------------------------------------------------
    
    $flt_start = microtime(true);
    
    $obj = new Common_DataTable();
    $obj->setCaption('Simple Table');
    $obj->setHeaders($arr_headers);
    $obj->bindData($obj_model_object);
    $obj->setSource('Database Object');
    
    $obj->sortDataBy(array(
        'group_name' => Utility_SortFunctions::TYPE_STRING,
        'value'      => Utility_SortFunctions::TYPE_NUMBER,
    ));
    $obj->groupDataBy(array('group_id' => 'group_name'));
    
    $str_render = $obj->render();   
    
    $flt_end = microtime(true);
    
    $arr_rendered[] = array(
        'time' => $flt_end - $flt_start,
        'echo' =>  $str_render     
    );
    /*
    //--------------------------------------------------------------------------
    
    $flt_start = microtime(true);
    
    $obj = new Common_DataTable();
    $obj->setCaption('Simple Table');
    $obj->setHeaders($arr_headers);
    $obj->bindData($arr_data);
    $obj->setSource('Array');
    
    $str_render = $obj->render();   
    
    $flt_end = microtime(true);
    
    $arr_rendered[] = array(
        'time' => $flt_end - $flt_start,
        'echo' =>  $str_render     
    );
    
    //--------------------------------------------------------------------------
    
    $flt_start = microtime(true);
    
    $obj = new Common_DataTable();
    $obj->setCaption('Complex Table');
    $obj->setHeaders($arr_headers);
    $obj->bindData($arr_data);
    $obj->setSource('Array');
    
    //---------
    
    $obj->sortDataBy(array(
        'group_name' => Utility_SortFunctions::TYPE_STRING,
        'value'      => Utility_SortFunctions::TYPE_NUMBER,
    ));
    $obj->groupDataBy(array('group_id' => 'group_name'));
    
    //----------
    
    $obj_summary = new Common_DataTable_Summary_Sum(array(
        'name' => 'Total'
    ));
    $obj_summary->setStaticValuesForGroup(array(
        'id' => '(__COUNT__) Items',
        'name' => 'Sub-Total for __GROUP__'
    ));
    
    $obj->addDataSummary($obj_summary);
    
    //---------
    
    $obj_summary = new Common_DataTable_Summary_Custom(array(
        'id' => '(__COUNT__)',
        'name' => 'Custom Group __GROUP__'
    ), 0
    );
    
    $obj_summary->setPositionType(Common_DataTable_Summary::POSITION_HEADER);
    
    $obj->addDataSummary($obj_summary);
    
    //---------
    
    $obj_summary = new Common_DataTable_Summary_Custom(array(
        'id' => '(__COUNT__)',
        'name' => 'Custom Footer'
    ));
    
    $obj->addDataSummary($obj_summary);
    
    //---------
    
    $str_render = $obj->render();
    
    $flt_end = microtime(true);
    
    $arr_rendered[] = array(
        'time' => $flt_end - $flt_start,
        'echo' =>  $str_render
    );
    
    //--------------------------------------------------------------------------
    
    $flt_start = microtime(true);
    
    $obj = new Common_DataTable();
    $obj->setCaption('Simple Table');
    $obj->setHeaders($arr_headers);
    $obj->bindData(new Common_DataTable_Model_DatabaseTable($obj_datagateway, 'test_huge'));
    $obj->setSource('Database Table');
    
    $str_render = $obj->render();
    $flt_end = microtime(true);
    
    $arr_rendered[] = array(
        'time' => $flt_end - $flt_start,
        'echo' =>  $str_render      
    );
    
    //--------------------------------------------------------------------------
    
    $flt_start = microtime(true);
    
    $obj = new Common_DataTable();
    $obj->setCaption('Complex Table');
    $obj->setHeaders($arr_headers);
    $obj->bindData(new Common_DataTable_Model_DatabaseTable($obj_datagateway, 'test_huge'));
    $obj->setSource('Database Table');
    
    $obj->sortDataBy(array(
        'value'      => Utility_SortFunctions::TYPE_NUMBER,
    ));
    $obj->groupDataBy(array('group_id' => 'group_name'));
    
    $obj_summary = new Common_DataTable_Summary_Sum(array(
        'name' => 'Total'
    ));
    $obj_summary->setStaticValuesForGroup(array(
        'id' => '(__COUNT__) Items',
        'name' => 'Sub-Total for __GROUP__'
    ));
    
    $obj->addDataSummary($obj_summary);
    
    //---------
    
    $obj_summary = new Common_DataTable_Summary_Custom(array(
        'id' => '(__COUNT__)',
        'name' => 'Custom Group __GROUP__'
    ), 0
    );
    
    $obj_summary->setPositionType(Common_DataTable_Summary::POSITION_HEADER);
    
    $obj->addDataSummary($obj_summary);
    
    //---------
    
    $obj_summary = new Common_DataTable_Summary_Custom(array(
        'id' => '(__COUNT__)',
        'name' => 'Custom Footer'
    ));
    
    $obj->addDataSummary($obj_summary);
    
    $flt_end = microtime(true);
    
    $arr_rendered[] = array(
        'time' => $flt_end - $flt_start,
        'echo' =>  $str_render           
    );
    
    
    
    //==========================================================================
    
    /*$obj = new Common_DataTable();
    $obj->setCaption('My Table');
    
    $obj->setHeaders(array(array(
        'id' => 'ID',
        'name' => 'Name',
        'group' => 'Group',
        'value' => 'Value'
    )));
    
    //$obj->bindData(new Common_DataTable_Model_DatabaseTable($obj_datagateway, $str_table_source));
    
    $obj->bindData(dirname(__FILE__).'/../data/sample.csv', array(
        'number_of_headers' => 2
    ));
    
    $obj->sortDataBy(array('value' => Utility_SortFunctions::TYPE_NUMBER));
    $obj->groupDataBy('group');
    
    $str_render2 = $obj->render();
    */
    $i = 0;
    foreach($arr_rendered as $arr) {
        echo '<div style="float:left;padding:1em;">';
        echo '<div>Time: ' . $arr['time'] . '</div>';
        echo $arr['echo'];
        echo '</div>';
        
        $i++;
        
        if(($i%2) == 0) echo '<div style="clear:both;"></div>';
    }
    
    ?>
</body>
</html>

